<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="searchForm">
           <el-form-item label="科目">
          <el-input
            v-model="searchForm.subject"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item v-if="userInfo.type==='1' || userInfo.type==='2'" label="学号">
          <el-input
            v-model="searchForm.studentAccountNumber"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="试卷标题">
          <el-input
            v-model="searchForm.paperTitle"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getData()">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据列表-->
      <el-table
        :data="tableData.slice((currentPage-1)*PageSize,currentPage*PageSize)"
        border
        class="table"
        ref="multipleTable"
        highlight-current-row
        
      >
        <el-table-column type="expand">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="题号:">
            <span>{{ props.row.question.questionNumber }}</span>
          </el-form-item>
          <el-form-item label="题目:">
            <span>{{ props.row.question.title }}</span>
          </el-form-item>
          <el-form-item label="选项:">
            <span>A:{{ props.row.question.optionA }}</span>&nbsp;&nbsp;&nbsp;
            <span>B:{{ props.row.question.optionB }}</span>&nbsp;&nbsp;&nbsp;
            <span>C:{{ props.row.question.optionC }}</span>&nbsp;&nbsp;&nbsp;
            <span>D:{{ props.row.question.optionD }}</span>&nbsp;&nbsp;&nbsp;
            <span>E:{{ props.row.question.optionE }}</span>&nbsp;&nbsp;&nbsp;
            <span>F:{{ props.row.question.optionF }}</span>&nbsp;&nbsp;&nbsp;
          </el-form-item>
          <el-form-item label="答题笔记：">
            <span>{{ props.row.note }}</span>
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="openDialog('编辑笔记内容', props.row)"
              style="margin-right:5px"
              >
            </el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
    <el-table-column
          prop="id"
          label="答题记录编号"
          min-width="50"
        ></el-table-column>
        <el-table-column
          prop="paperTitle"
          label="试卷标题"
          min-width="50"
        ></el-table-column>
        <el-table-column
          prop="questionId"
          label="试题编号"
          min-width="30"
        ></el-table-column>
        <el-table-column
          prop="subject"
          label="科目"
          min-width="50"
        ></el-table-column>
        <el-table-column
          prop="answer"
          label="所选答案"
          min-width="30"
        ></el-table-column>
        <el-table-column
          prop="score"
          label="所得分值"
          min-width="40"
        ></el-table-column>
        <el-table-column
          label="操作"
          min-width="150"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-popconfirm
                confirm-button-text='确定'
                cancel-button-text='取消'
                icon="el-icon-info"
                @onConfirm="deleteRecord(scope.row)"
                icon-color="red"
                title="确定删除该记录吗？"
                >
                <el-button
              type="text"
              slot="reference"
              icon="el-icon-delete"
              class="red"
              >删除
            </el-button>
            </el-popconfirm>
            
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.length"
        >
        </el-pagination>
      </div>
      <!--修改 模态框-->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form
          :model="editTestinfoForm"
          ref="editTestinfoForm"
          :rules="testInfoRules"
          label-width="120px"
          :inline="true"
        >
          <el-form-item label="答题记录编号:">
            <el-input v-model="editTestinfoForm.id" ></el-input>
          </el-form-item>
          <el-form-item label="答题笔记:">
            <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入内容"
              v-model="editTestinfoForm.note">
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="saveEdit"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import webservice from '../../../api/webservice'
export default {
  data() {
    return {
      tableData: [],
      // 默认显示第几页
        currentPage: 1,
        // 个数选择器（可修改）
        pageSizes: [10, 20,30,40,50],
        // 默认每页显示的条数（可修改）
        PageSize: 10,
      searchForm: {}, //查询对象
      dialogFormVisible: false, //添加或修改的模态框标识
      dialogTitle: "",
      editTestinfoForm: {}, //编辑人员信息的表单信息
      testInfoRules:{
          name: [
                { required: true, message: '请输入昵称', trigger: 'blur' },
            ],
      },
        questionList:[],//试题
        classList:[],
        userInfo:{}
    };
  },
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.getData();
    this.getClassData()
  },
  methods: {
    // 分页
    // 每页显示的条数
    handleSizeChange(val) {
        // 改变每页显示的条数 
        this.PageSize = val
        console.log(this.pageSizes)
        // 注意：在改变每页显示的条数时，要将页码显示到第一页
        this.currentPage = 1
    },
    // 显示第几页
    handleCurrentChange(val) {
        // 改变默认的页数
        this.currentPage = val
    },
    getData() {
      if(this.userInfo.type==='0'){
        var _param = {
        subject:this.searchForm.subject,
        studentAccountNumber:this.userInfo.accountNumber,
        paperTitle:this.searchForm.paperTitle
      };
      }else{
        var _param = {
        subject:this.searchForm.subject,
        studentAccountNumber:this.searchForm.studentAccountNumber,
        paperTitle:this.searchForm.paperTitle
      };
      }
       let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/answerRecord/selectAnswerRecordList", _params, (data) => {
        if (data.code === 200) {
          this.tableData = data.data;
        //   this.questionList = this.tableData.questionList
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //获取班级
        getClassData() {
      webservice.HttpPost("/api/clazz/selectClazzList", '{}', (data) => {
        if (data.code === 200) {
          this.classList = data.data;
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //打开 编辑 模态框
    openDialog(flag, row) {
      this.dialogTitle = flag;
      this.editTestinfoForm = JSON.parse(JSON.stringify(row));
      this.dialogFormVisible = true;
    },
    //修改
    saveEdit() {
      let _param = {
        id:this.editTestinfoForm.id,
        note:this.editTestinfoForm.note,
      };
      let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/answerRecord/updateAnswerRecord", _params, (data) => {
        if (data.code === 200) {
          this.dialogFormVisible = false;
          this.getData()
          this.$message.success({
            message: '修改成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.dialogFormVisible = false;
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
    //删除
    deleteRecord(row){
      let _param = {
        id: row.id
      };
      let _params = JSON.stringify(_param)
      webservice.HttpPost("/api/answerRecord/deleteAnswerRecord", _params, (data) => {
        if (data.code === 200) {
            this.getData()
            this.$message.success({
            message: '删除成功！',
            showClose: true,
            duration: 1000,
          });
        } else {
          this.$message.error({
            message: data.message,
            showClose: true,
            duration: 1000,
          });
        }
      });
    },
  },
};
</script>
<style>
 .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    /* width: 50px; */
    color: #99a9bf;
  }
  .el-form-item__label{
      padding: 0;
  }
  .demo-table-expand .el-form-item {
    margin-right: 8px;
    margin-bottom: 0;
    width: 50%;
  }
</style>